/**
  ******************************************************************************
  * @file           : XYXKC_Serial_ml307r.h
  * @author         : admin
  * @brief          : None
  * @attention      : None
  * @date           : 2024/11/24
  ******************************************************************************
  */

#ifndef XYXKC_SERIAL_ML307R_H
#define XYXKC_SERIAL_ML307R_H
#include <SoftwareSerial.h>
#include <HardwareSerial.h>
class XYXKC_Serial_ml307r {
    public:
    XYXKC_Serial_ml307r();
    ~XYXKC_Serial_ml307r();
    void begin(SoftwareSerial *serial,int baudRate = 9600);
    void begin(HardwareSerial *serial,int baudRate = 9600);
    void begin(HardwareSerial *serial,int rx,int tx,int baudRate = 9600);
    void end();
    int read();
    int available();
    size_t write(uint8_t byte);
    size_t write(unsigned long n) { return write((uint8_t)n); }
    size_t write(long n) { return write((uint8_t)n); }
    size_t write(unsigned int n) { return write((uint8_t)n); }
    size_t write(int n) { return write((uint8_t)n); }

    size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
    size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
    size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
    size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); }
    String readString();
    String readStringUntil(char terminator);
    void flush();

    template <typename T>
    void print(T a){
        _isHardware? _hardware->print(a):_software->print(a);
    }
    template <typename T>
    void println(T a){
        _isHardware? _hardware->println(a):_software->println();
    }

    private:
        SoftwareSerial *_software;
        HardwareSerial *_hardware;
        bool _isHardware;

};


#endif //XYXKC_K210_XYXKC_SERIAL_H
